package start.spring.basic.demo.service.demo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.demo.dao.TestHMapper;
import start.spring.basic.demo.entity.TestH;
import start.spring.basic.demo.service.BaseServiceImpl;
import start.spring.basic.demo.service.demo.IH2DemoService;
import start.spring.basic.dynamic.DsConstant;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.util.common.StringUtil;

/**
 * ****************************************************************************
 * h2内存数据库使用业务处理实现
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2023年2月28日
 ******************************************************************************
 */
@Service
public class H2DemoServiceImpl extends BaseServiceImpl implements IH2DemoService {

	@Autowired
	TestHMapper testHMapper;
	
	@DS(DsConstant.H2)
	@Override
	public ResponseData<String> mpAdd() {
		TestH test1 = new TestH();
		test1.setA("a1");
		test1.setB("b1");
		
		this.testHMapper.insert(test1);
		
		TestH test2 = new TestH();
		test2.setA("a2");
		test2.setB("b2");
		this.testHMapper.insert(test2);
		
		TestH test3 = new TestH();
		test3.setA("a3");
		test3.setB("b3");
		this.testHMapper.insert(test3);
		
		return ResponseData.success();
	}

	@DS(DsConstant.H2)
	@Override
	public PageResult<TestH> mpMultiDbQueryPage(PageQuery pageQuery, String a) {
		IPage<TestH> page = super.getPage(pageQuery);
		QueryWrapper<TestH> queryWrapper = new QueryWrapper<>();
		if(!StringUtil.isEmpty(a)) {
			queryWrapper.eq("a", a);
		}
		page = this.testHMapper.selectPage(page, queryWrapper);
		return super.getPageResult(page);
	}

	@DS(DsConstant.H2)
	@Override
	public ResponseData<String> mpDel() {
		QueryWrapper<TestH> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("a", "a2");
		this.testHMapper.delete(queryWrapper);
		return ResponseData.success();
	}
}
